package com.hc.conferencecheck.filters;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用于解决跨域问题的过滤器,所以这个过滤器的优先级是最高的（废弃，因为已不存在跨域问题）
 */
//@WebFilter(filterName = "corsFilter", urlPatterns = {"/*"})
public class Filter0_CorsFilter implements Filter {
    final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Filter0_CorsFilter.class);

    public void init(FilterConfig filterConfig) {
        logger.info("初始化解决跨域问题的过滤器");
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        logger.info("进入解决跨域问题的过滤器");
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        response.setContentType("textml;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "0");
        response.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("XDomainRequestAllowed", "1");
        System.out.println("*********************************跨域过滤器被使用**************************");
        chain.doFilter(req, res);
    }

    public void destroy() {
    }
}
